package jjn.carl.backtrack;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Jiang Jining
 * @since 2023-07-23 22:19
 */
public class LeetCode216 {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result = new ArrayList<>();
        backtrack(k, n, new ArrayList<>(), result, 0, 1);
        return result;
    }
    
    private void backtrack(int k, int n, List<Integer> path, List<List<Integer>> result, int sum, int startIndex) {
        if (path.size() > k || sum > n) {
            return;
        }
        if (path.size() == k && sum == n) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < 10; i++) {
            path.add(i);
            sum += i;
            backtrack(k, n, path, result, sum, i + 1);
            path.remove(path.size() - 1);
            sum -= i;
        }
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<List<Integer>> list = new LeetCode216().combinationSum3(k, n);
        System.out.println(String.join(",", list.toString()));
    }
}
